package com.github.wxiaoqi.security.biz.biz.restaurant;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.wxiaoqi.security.biz.entity.restaurant.RestaurantOrderInfo;
import com.github.wxiaoqi.security.biz.entity.restaurant.RestaurantShoppingCartInfo;
import com.github.wxiaoqi.security.biz.entity.restaurant.RestaurantShoppingDishesInfo;
import com.github.wxiaoqi.security.biz.entity.restaurant.RestaurantTableInfo;
import com.github.wxiaoqi.security.biz.mapper.RestaurantOrderInfoMapper;
import com.github.wxiaoqi.security.biz.mapper.RestaurantShoppingCartInfoMapper;
import com.github.wxiaoqi.security.biz.mapper.RestaurantTableInfoMapper;
import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.msg.ResponseCode;
import com.github.wxiaoqi.security.common.util.EntityUtils;

/**
 * 订单表
 *
 * @author he
 * @date 2018-06-29 10:31:27
 */
@Service
public class RestaurantOrderInfoBiz extends BaseBiz<RestaurantOrderInfoMapper,RestaurantOrderInfo> {
	
	private static final Logger logger = (Logger) LoggerFactory.getLogger(RestaurantOrderInfoBiz.class);
	
	@Autowired
	private RestaurantOrderInfoMapper restaurantOrderInfoMapper;
	@Autowired
	private RestaurantShoppingCartInfoMapper restaurantShoppingCartInfoMapper;
	@Autowired
	private RestaurantTableInfoMapper restaurantTableInfoMapper;
	
	/**
	 * 查询点餐订单信息
	 * 默认查询当天最后一条订单信息及该用户购物车全部已支付的商品
	 * @param request
	 * @return
	 */
	public Map<String, Object> queryOrderPayOrderInfo(Map<String, Object> request){
		logger.info("查询点餐订单信息请求。。。。。。{}", request);
		Map<String, Object> response = new HashMap<String, Object>();
		String platformId = (String) request.get("platformId");
		String merchantId = (String) request.get("merchantId");
		String customerId = (String) request.get("customerId");
		RestaurantOrderInfo restaurantOrderInfo = new RestaurantOrderInfo();
		restaurantOrderInfo.setPlatformId(platformId);
		restaurantOrderInfo.setMerchantId(merchantId);
		restaurantOrderInfo.setCustomerId(customerId);
		restaurantOrderInfo.setStatus("00");//支付成功
		restaurantOrderInfo.setType("1");//店内点餐
		RestaurantOrderInfo todayLastOrderInfo = restaurantOrderInfoMapper.queryTodayLastOrderInfo(restaurantOrderInfo);
		Map<String, Object> map = EntityUtils.beanToMap(todayLastOrderInfo);
		if(null != todayLastOrderInfo){
			String tableId = todayLastOrderInfo.getTableId();
			RestaurantTableInfo restaurantTableInfo = new RestaurantTableInfo();
			restaurantTableInfo.setPlatformId(platformId);
			restaurantTableInfo.setMerchantId(merchantId);
			restaurantTableInfo.setTableId(tableId);
			RestaurantTableInfo selectRestaurantTableInfo = restaurantTableInfoMapper.selectOne(restaurantTableInfo);
			if(null != selectRestaurantTableInfo){
				String tableName = selectRestaurantTableInfo.getTableName();
				map.put("tableName", tableName);
			}
			
			//查询已评价购物车
			RestaurantShoppingCartInfo restaurantShoppingCartInfo = new RestaurantShoppingCartInfo();
			restaurantShoppingCartInfo.setPlatformId(platformId);
			restaurantShoppingCartInfo.setMerchantId(merchantId);
			restaurantShoppingCartInfo.setPerId(customerId);
			restaurantShoppingCartInfo.setType("1");//店内点餐
			restaurantShoppingCartInfo.setStatus("3");//已支付
			restaurantShoppingCartInfo.setIsAppraisal("0");//已评价
			restaurantShoppingCartInfo.setPayOrderNo(todayLastOrderInfo.getPayOrderNo());
			List<RestaurantShoppingDishesInfo> appraisaledInfo = restaurantShoppingCartInfoMapper.queryRestaurantDishesInfo(restaurantShoppingCartInfo);
			response.put("appraisaledInfo", appraisaledInfo);
			
			//查询未评价购物车
			restaurantShoppingCartInfo.setIsAppraisal("1");//未评价
			List<RestaurantShoppingDishesInfo> unAppraisalInfo = restaurantShoppingCartInfoMapper.queryRestaurantDishesInfo(restaurantShoppingCartInfo);
			response.put("unAppraisalInfo", unAppraisalInfo);
		}else{
			response.put("appraisaledInfo", null);
			response.put("unAppraisalInfo", null);
		}
		response.put("restaurantOrderInfo", map);
		response.put("code", ResponseCode.OK.getCode());
		response.put("msg", ResponseCode.OK.getMessage());
		logger.info("查询点餐订单信息响应。。。。。。{}", response);
		return response;
	}
	
	/**
	 * 查询外卖订单信息
	 * @param request
	 * @return
	 */
	public Map<String, Object> queryTakeawayPayOrderInfo(Map<String, Object> request){
		logger.info("查询外卖订单信息请求。。。。。。{}", request);
		Map<String, Object> response = new HashMap<String, Object>();
		String platformId = (String) request.get("platformId");
		String merchantId = (String) request.get("merchantId");
		String customerId = (String) request.get("customerId");
		
		//查询未确认订单
		RestaurantOrderInfo restaurantOrderInfo = new RestaurantOrderInfo();
		restaurantOrderInfo.setPlatformId(platformId);
		restaurantOrderInfo.setMerchantId(merchantId);
		restaurantOrderInfo.setCustomerId(customerId);
		restaurantOrderInfo.setStatus("00");//支付成功
		restaurantOrderInfo.setType("3");//外卖
		restaurantOrderInfo.setIsConfirm("1");//未确认
		List<RestaurantOrderInfo> noConfirmList = restaurantOrderInfoMapper.select(restaurantOrderInfo);
		response.put("noConfirmList", noConfirmList);
		
		//查询未评论订单
		restaurantOrderInfo.setIsConfirm("0");//已确认
		restaurantOrderInfo.setIsAppraisal("1");//未评论
		List<RestaurantOrderInfo> confirmedList = restaurantOrderInfoMapper.select(restaurantOrderInfo);
		response.put("confirmedList", confirmedList);
		
		//查询已完成
		restaurantOrderInfo.setIsAppraisal("0");//已评论
		List<RestaurantOrderInfo> completedList = restaurantOrderInfoMapper.select(restaurantOrderInfo);
		response.put("completedList", completedList);
		
		//查询完成订单
		response.put("code", ResponseCode.OK.getCode());
		response.put("msg", ResponseCode.OK.getMessage());
		logger.info("查询外卖订单信息响应。。。。。。{}", response);
		return response;
	}
	
}